<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 组织架构内容   头部 -->
      <el-card class="tree-card">
        <!-- 放置结构内容 -->
        <tree-tools
          :treeNode="company"
          isRoot
          @addDepts="addDepts"
        ></tree-tools>
        <!-- 树形结构 -->
        <el-tree :data="departs" default-expand-all :props="defaultProps">
          <!-- 传入内容 用插槽对departs进行循环  -->
          <!-- 作用域插槽 slot-scope='obj' 接受传递给插槽的数据 data 每个节点插槽的内容的数据 -->
          <tree-tools
            slot-scope="{ data }"
            :treeNode="data"
            @delDepts="getDepartments"
            @addDepts="addDepts"
            @editDepts="editDepts"
          ></tree-tools>
        </el-tree>
      </el-card>
    </div>
    <add-dept
      ref="addDept"
      :showDialog.sync="showDialog"
      :treeNode="node"
      @addDepts="getDepartments"
    ></add-dept>
  </div>
</template>

<script>
import TreeTools from './components/tree-tools.vue'
import AddDept from './components/add-dept.vue'
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import { Loading } from 'element-ui'
export default {
  components: {
    TreeTools,
    AddDept
  },
  data() {
    return {
      company: { name: '', manager: '负责人', id: '' },
      departs: [],
      defaultProps: {
        label: 'name' // 表示 从这个属性显示内容
      },
      showDialog: false, //默认隐藏添加弹出层
      node: null, //记录当前点击的Node节点
      loading: false
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      this.loading = true
      const result = await getDepartments()
      this.company = { name: result.companyName, manager: '负责人', id: '' }
      this.departs = tranListToTreeData(result.depts, '')
      this.loading = false
    },
    // 监听tree-tools中触发的点击添加子部门的事件
    // node是点击的部门
    addDepts(node) {
      this.showDialog = true //显示弹层
      this.node = node
    },
    editDepts(node) {
      this.showDialog = true
      this.node = node
      this.$refs.addDept.getDepartDetail(node.id)
    }
  }
}
</script>
<style scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>
